/**
    Copyright (c) 2010-2011 yakiimo02
    Distributed under the New BSD License.
    See included license.txt or http://www.yakiimo3d.com/NewBSDLicense.txt
**/

#include "DXUT.h"
#include "MandelbrotBase.h"

MandelbrotBase::MandelbrotBase() : m_vCenter( 0.0f, 0.0f ), m_fZoomScale( 1.0f ), m_nMaxIterations( 256 )
{
}

MandelbrotBase::~MandelbrotBase()
{
}

void MandelbrotBase::TranslateCenter( const D3DXVECTOR2& vDeltaCenter )
{    
    m_vCenter += ( vDeltaCenter * m_fZoomScale );
}

void MandelbrotBase::ZoomScale( const float fDeltaScale )
{
    m_fZoomScale *= fDeltaScale;
    if( m_fZoomScale == 0.0f ) {
        m_fZoomScale = 0.000001f;
    }
}

void MandelbrotBase::UpdateConstantBuf( int nWidth, int nHeight )
{
    // decide the bounds of the complex number plane
    m_cb.fMinRe = ( -2.0f *  m_fZoomScale ) + m_vCenter.x;
    m_cb.fMaxRe = ( 1.0f * m_fZoomScale ) + m_vCenter.x;
    m_cb.fMinIm = ( -1.2f * m_fZoomScale ) + m_vCenter.y;
    // maintain same width/height ratio as the data buffer
    m_cb.fMaxIm = m_cb.fMinIm+(m_cb.fMaxRe-m_cb.fMinRe)*nHeight/nWidth;
    
    // step size for the complex number plane
    m_cb.fStepRe = (m_cb.fMaxRe-m_cb.fMinRe)/(nWidth-1);
    m_cb.fStepIm = (m_cb.fMaxIm-m_cb.fMinIm)/(nHeight-1);    

    m_cb.nSize = nWidth*nHeight;

    m_cb.nWidth = nWidth;

    // maximum iterations to decide whether an complex number is in the mandelbrot set.
    m_cb.nMaxIterations = m_nMaxIterations;
}